<?php

namespace addons\xykeep\listener;

use addons\xykeep\service\Distribution as DistributionService;
use addons\xykeep\service\Store as StoreService;
use addons\xykeep\service\distribution\Commission;
use app\admin\model\xykeep\user\Level;
use app\api\model\xykeep\goods\Order as GoodsOrderModel;
use think\Db;

/**
 * 分销钩子处理
 */
class Distribution
{

    // 订单支付成功 
    public function xykeepOrderPayedAfter(&$params){

        $orderData = $params['order'];
        $orderType = $params['type'];

        // 重新查询订单
        $order = null;


        // 商城订单实际用的是一个订单表
        if ($orderType == 'goods') {
            $order = GoodsOrderModel::where(['id'=>$orderData['id']])->find();
        }


        if (!$order) return false;

        $distributon = new DistributionService($order['user_id']);

        if (!$distributon->user)  return false;

        // 加用户消费金额 并自动升级
        if ($order['total_fee'] > 0) {
            //用户等级
            $this_level = Level::where('id',$distributon->user['level'])->find();
            //下个等级
            $next_level = Level::where(['consume'=>['gt',$this_level['consume']]])->order('consume asc')->find();
            if($next_level){
                $after_consume = bcadd((String)$distributon->user['xykeep_consume'],(String)$order['total_fee'],2);
                if($after_consume > $next_level['consume']){
                    Db::name('user')->where([
                        'id' => $distributon->user['id'],
                    ])->update([
                        'level' => $next_level['id'],
                    ]);
                }
            }
            $distributon->user->setInc('xykeep_consume', $order['total_fee']);
        }

        // 绑定关系
        $distributon->bindUserRelation('pay');
        

        // 记录、处理分佣升级
        $commission = new Commission($order,$orderType);
        // 检查能否分销
        if ($commission->checkAndSetCommission()) {
            // 添加分销订单
            $distributionOrder = $commission->addDistributionOrder();
            // 支付后分佣
            $commission->grantDistributionOrder('payed', $distributionOrder->id);
        }

        //增加商家收入
        if(in_array($orderType, ['goods'])){
            // 添加门店收入和记录，现在记录的是纯收入，即用户支付多少，商家收到多少
            $storeService =  new StoreService(null,$order['store_id']);
            $storeService->commission($order['total_fee'],'order',$order['id']);
        }
    }
    
    /**
     * 用户注册账号之后，添加分销账号
     */
    public function xykeepRegisterAfter($user){
        if($user){
            $distribution = new DistributionService($user->id);
            $distribution->becomeDistribution();
        }
    }

    /**
     * 用户通过分享进入后,绑定用户上级
     */
    public function xykeepShareAfter($shareLog){
        if ($shareLog) {
            $user_id = intval($shareLog->user_id); // 受邀用户
            $share_id = intval($shareLog->share_id); // 邀请人

            $distribution = new DistributionService($user_id);
            $distribution->bindUserRelation('share', $share_id); //绑定关系
            
        }
    }
    
}
